package ua.epam.diff;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.PDFImageWriter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class PDFComparator {

    public static int compare(String pathToOrigin, String pathToCopy) throws IOException {

        PDDocument document = PDDocument.load(new File(pathToOrigin));
        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        int resolution = Toolkit.getDefaultToolkit().getScreenResolution();
        for (int i = 0; i < pages.size(); i++) {
          //  PDPage singlePage = pages.get(i);
            PDFImageWriter imageWriter = new PDFImageWriter();

            boolean success = imageWriter.writeImage(document, "jpg", "",
                       i+1 , i+1, "origin"+i,BufferedImage.TYPE_BYTE_GRAY,resolution*5);
            //ImageIO.write(buffImage, "png", new File(PdfUtil.DATA_OUTPUT_DIR + (count++) + ".png"));
        }
        document.close();

        document = PDDocument.load(new File(pathToCopy));
        pages = document.getDocumentCatalog().getAllPages();
        resolution = Toolkit.getDefaultToolkit().getScreenResolution();
        for (int i = 0; i < pages.size(); i++) {
          //  PDPage singlePage = pages.get(i);
            PDFImageWriter imageWriter = new PDFImageWriter();
            boolean success = imageWriter.writeImage(document, "jpg", "",
                       i+1 , i+1, "copy"+i, BufferedImage.TYPE_BYTE_GRAY,resolution*5);
            //ImageIO.write(buffImage, "png", new File(PdfUtil.DATA_OUTPUT_DIR + (count++) + ".png"));
        }
        document.close();

        return pages.size();

    }

}